/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.faesa.acervo.business.ejb;

import br.faesa.acervo.entity.Aluno;
import br.faesa.acervo.entity.Livro;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.NoResultException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author Eric Rosetti Lessa
 */
@Stateless
@LocalBean
public class AlunoDAO extends AcervoDAO<Livro>{
    
    public AlunoDAO(){
        super(Livro.class);
    }
    
    public Aluno findByMatricula(Long matricula){
        
        CriteriaBuilder cb = super.em.getCriteriaBuilder();
        
        CriteriaQuery<Aluno> cq = cb.createQuery(Aluno.class);
        
        Root r = cq.from(Aluno.class);
        
        cq.where(cb.equal(r.get("matricula"), matricula));
        
        cq.select(r);

        try{
            return super.em.createQuery(cq).getSingleResult();
        }catch(NoResultException e){
            return null;
        }
        
    }
}
